import socket
import datetime
import tcp_add_pb2

def all_bytes():
    person = tcp_add_pb2.Person()
    person.name = send_name
    person.time = send_time
    person.show = send_show
    # 序列化
    return person.SerializeToString()

if __name__ == '__main__':
    # 1.创建tcp端套接字
    # AF_INEF表示ipv4的地址类型
    # SOCK_STREAMM:tcp传输协议类型
    tcp_client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    # 2.和服务端建立连接
    tcp_client_socket.connect(("127.0.0.1", 9090))
    while True:
        send_name = input("姓名")
        send_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        send_show = input("说:")
        hearBeat_data = all_bytes()
        # 3.将数据发送到服务端
        tcp_client_socket.send(hearBeat_data)
        # 4.接收服务端的数据
        # 1024表示每次接收的最大字节
        Head_data = tcp_client_socket.recv(1024)
        tmessage = tcp_add_pb2.Person()
        # 将信息反序列化
        tmessage.ParseFromString(Head_data)
        m_name = tmessage.name
        m_time = tmessage.time
        m_show = tmessage.show
        print(f'姓名:{m_name}在{m_time}时说{m_show}')

    # 5.关闭套接字
    tcp_client_socket.close()